package com.genesis.yunnanji.view;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.CollectionAdapter;
import com.genesis.yunnanji.bean.CollectionBean;
import com.genesis.yunnanji.widget.RecycleViewDivider;
import com.timmy.tdialog.TDialog;
import com.timmy.tdialog.base.BindViewHolder;
import com.timmy.tdialog.listener.OnBindViewListener;
import com.timmy.tdialog.listener.OnViewClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2018/9/28 002815:17
 * 作者：Kite
 * 描述：我的收藏
 */
public class UserCollectionActivity extends BaseActivity implements CollectionAdapter.OnItemClickListener {

    @BindView(R.id.rv_collection)
    RecyclerView rvCollection;

    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;

    @BindView(R.id.rl_choose)
    RelativeLayout rlChoose;

    @BindView(R.id.tv_select_all)
    TextView tvSelectAll;

    @BindView(R.id.iv_choose)
    ImageView ivChoose;

    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private CollectionAdapter mCollectionAdapter;

    private List<CollectionBean> mCollectionBeanList = new ArrayList<>();

    private boolean editorStatus = false;

    private int index = 0;

    private boolean isSelectAll = false;

    private static final int MYLIVE_MODE_CHECK = 0;
    private static final int MYLIVE_MODE_EDIT = 1;
    private int mEditMode = MYLIVE_MODE_CHECK;


    @Override
    public int getLayoutId() {
        return R.layout.activity_user_collection;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("我的收藏");
        getDate();
        addMenuTextItem(R.string.edit, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updataEditMode();
            }
        });
        mCollectionAdapter.setOnItemClickListener(this);
    }

    @OnClick({R.id.rl_choose, R.id.tv_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_choose:
                selectAllMain();
                break;
            case R.id.tv_cancel:
                cancelCollection();
                break;
        }
    }

    /**
     * 全选和反选
     */
    private void selectAllMain() {
        if (mCollectionAdapter == null) return;
        if (!isSelectAll) {
            for (int i = 0, j = mCollectionBeanList.size(); i < j; i++) {
                mCollectionBeanList.get(i).setSelect(true);
            }
            index = mCollectionBeanList.size();
            ivChoose.setBackgroundResource(R.drawable.icon_choose);
            tvSelectAll.setText("取消全选");
            isSelectAll = true;
        } else {
            for (int i = 0, j = mCollectionBeanList.size(); i < j; i++) {
                mCollectionBeanList.get(i).setSelect(false);
            }
            index = 0;
            tvSelectAll.setText("全选");
            ivChoose.setBackgroundResource(R.drawable.icon_unchoose);
            isSelectAll = false;
        }
        mCollectionAdapter.notifyDataSetChanged();
        setBtnBackground(index);
    }

    private void updataEditMode() {
        if (mCollectionAdapter != null) {
            for (int i = 0, j = mCollectionBeanList.size(); i < j; i++) {
                mCollectionBeanList.get(i).setSelect(false);
            }
            index = 0;
            tvSelectAll.setText("全选");
            ivChoose.setBackgroundResource(R.drawable.icon_unchoose);
            isSelectAll = false;
        }
        mEditMode = mEditMode == MYLIVE_MODE_CHECK ? MYLIVE_MODE_EDIT : MYLIVE_MODE_CHECK;
        if (mEditMode == MYLIVE_MODE_EDIT) {
//            mBtnEditor.setText("取消");
            rlBottom.setVisibility(View.VISIBLE);
            editorStatus = true;
        } else {
//            mBtnEditor.setText("编辑");
            rlBottom.setVisibility(View.GONE);
            editorStatus = false;
            clearAll();
        }
        mCollectionAdapter.setEditMode(mEditMode);
    }

    private void clearAll() {
        isSelectAll = false;
        tvSelectAll.setText("全选");
        setBtnBackground(0);
//        ivChoose.setBackgroundResource(R.drawable.icon_unchoose);
    }

    private void getDate() {
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        mCollectionBeanList.add(new CollectionBean("http://b.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=605eff43ba0e7bec238f0be51a1e950e/b3fb43166d224f4a79fb985f0bf790529822d118.jpg",
                "云南野生菌", "吃啥补啥", "100.99"));
        rvCollection.setLayoutManager(new LinearLayoutManager(this));
        mCollectionAdapter = new CollectionAdapter(this, mCollectionBeanList);
        rvCollection.setAdapter(mCollectionAdapter);
        rvCollection.setItemAnimator(new DefaultItemAnimator());
        rvCollection.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.color_eee)));
    }

    @Override
    public void onItemClickListener(int pos, List<CollectionBean> myLiveList) {
        if (rlBottom.getVisibility() == View.VISIBLE) {
            if (editorStatus) {
                CollectionBean myLive = myLiveList.get(pos);
                boolean isSelect = myLive.isSelect();
                if (!isSelect) {
                    index++;
                    myLive.setSelect(true);
                    if (index == myLiveList.size()) {
                        isSelectAll = true;
                        tvSelectAll.setText("取消全选");
                    }
                    ivChoose.setBackgroundResource(R.drawable.icon_choose);
                } else {
                    myLive.setSelect(false);
                    index--;
                    isSelectAll = false;
                    tvSelectAll.setText("全选");
                }
                setBtnBackground(index);
                mCollectionAdapter.notifyDataSetChanged();
            }
        } else {
            advance(AddDeliveryAddressActivity.class);
        }
    }

    private void cancelCollection() {
        if (index == 0) {
            tvCancel.setEnabled(false);
            return;
        }
        tvCancel.setEnabled(true);
        new TDialog.Builder(getSupportFragmentManager())
                .setLayoutRes(R.layout.dialog_show_tips)
                .setScreenWidthAspect(UserCollectionActivity.this, 0.96f)
                .setGravity(Gravity.CENTER)
                .setDialogAnimationRes(R.style.dialog_fragment_bottom_anim)
                .addOnClickListener(R.id.btn_sure, R.id.btn_cancel)
                .setOnBindViewListener(new OnBindViewListener() {
                    @Override
                    public void bindView(BindViewHolder viewHolder) {
                        viewHolder.setText(R.id.tv_msg, "确定要取消收藏选中数据？");
                    }
                })
                .setOnViewClickListener(new OnViewClickListener() {
                    @Override
                    public void onViewClick(BindViewHolder viewHolder, android.view.View view, TDialog tDialog) {
                        switch (view.getId()) {
                            case R.id.btn_sure:
                                for (int i = mCollectionBeanList.size(), j = 0; i > j; i--) {
                                    CollectionBean myLive = mCollectionBeanList.get(i - 1);
                                    if (myLive.isSelect()) {
                                        mCollectionBeanList.remove(myLive);
                                        index--;
                                    }
                                }
                                index = 0;
                                setBtnBackground(index);
                                if (mCollectionBeanList.size() == 0) {
                                    rlBottom.setVisibility(View.GONE);
                                }
                                mCollectionAdapter.notifyDataSetChanged();
                                tDialog.dismiss();
                                break;
                            case R.id.btn_cancel:
                                tDialog.dismiss();
                                break;
                        }
                    }
                })
                .create()
                .show();
    }

    /**
     * 根据选择的数量是否为0来判断按钮的是否可点击.
     *
     * @param size
     */
    private void setBtnBackground(int size) {
        if (size != 0) {
            tvCancel.setEnabled(true);
        } else {
            tvCancel.setEnabled(false);
        }
    }

}
